package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.counsellor.service.ICounsellorStudentStatisticService;
import com.ruoyi.statistic.domain.StatisticStudentEmploymentFile;
import com.ruoyi.statistic.service.IStatisticStudentEmploymentFileService;
import com.ruoyi.student.service.IStudentBaseInfoService;
import com.ruoyi.student.service.IStudentRecommendInfoService;
import com.ruoyi.system.domain.vo.AdminConsoleVo;
import com.ruoyi.counsellor.domain.vo.CounsellorConsoleVo;
import com.ruoyi.system.domain.vo.Recent10DaysRateVo;
import com.ruoyi.system.domain.vo.StudentRecommendCompletionStatus;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 首页
 *
 * @author ruoyi
 */
@RestController
public class SysIndexController extends BaseController {
    /**
     * 系统基础配置
     */
    @Resource
    private RuoYiConfig ruoyiConfig;


    @Resource
    private ISysConfigService sysConfigService;


    @Resource
    private IStatisticStudentEmploymentFileService statisticStudentEmploymentFileService;

    @Resource
    private IStudentBaseInfoService studentBaseInfoService;

    @Resource
    private IStudentRecommendInfoService studentRecommendInfoService;

    @Resource
    private ICounsellorStudentStatisticService counsellorStudentStatisticService;

    /**
     * 访问首页，提示语
     */
    @RequestMapping("/")
    public String index() {
        return StringUtils.format("请通过前端地址访问");
    }


    /**
     * 返回首页初始化数据
     *
     * @return
     */
    @RequestMapping("/admin/index/init")
    public AjaxResult adminInit() {
        //获取当前毕业年份
        String year = sysConfigService.selectConfigByKey("current.graduation.year");

        AdminConsoleVo vo = new AdminConsoleVo();
        //处理就业趋势
        Recent10DaysRateVo recent10DaysRateVo = new Recent10DaysRateVo();
        List<StatisticStudentEmploymentFile> studentEmploymentFileList = statisticStudentEmploymentFileService.selectRecentDaysFile(year, 8);
        recent10DaysRateVo.setLegend(new ArrayList<String>() {{
            add("签约率");
            add("就业率");
        }});

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        List<String> xAxisList = studentEmploymentFileList.stream().map(item -> {
            return sdf.format(item.getFileTime());
        }).collect(Collectors.toList());
        Collections.reverse(xAxisList);
        recent10DaysRateVo.setXAxis_data(xAxisList);


        /**
         * 构造签约率
         */
        List<String> signingRateList = studentEmploymentFileList.stream().map(item -> {
            return item.getSigningRate().substring(0, item.getSigningRate().length() - 1);
        }).collect(Collectors.toList());
        Collections.reverse(signingRateList);
        recent10DaysRateVo.setSigningRateList(signingRateList);

        /**
         * 构造就业率
         */
        List<String> employmentRateList = studentEmploymentFileList.stream().map(item -> {
            return item.getEmploymentRate().substring(0, item.getEmploymentRate().length() - 1);
        }).collect(Collectors.toList());
        Collections.reverse(employmentRateList);
        recent10DaysRateVo.setEmploymentRateList(employmentRateList);


        //获取最近一天日期的文件上传日期
        Date lastDate = statisticStudentEmploymentFileService.selectLastFileDate(year);//最后一次文件提交日期

        vo.setRecent10DaysRate(recent10DaysRateVo);
        vo.setCurrentYear(year);
        StatisticStudentEmploymentFile statisticStudentEmploymentFile = statisticStudentEmploymentFileService.selectStatisticStudentEmploymentFileByDate(year, lastDate);
        vo.setCurrentEmploymentRate(statisticStudentEmploymentFile.getEmploymentRate());
        vo.setCurrentSigningRate(statisticStudentEmploymentFile.getSigningRate());
        vo.setNationwideSystemCount(statisticStudentEmploymentFile.getStudentCount());


        //获取当前毕业年份的各系毕业生推荐表已完成人数和未完成人数
        List<StudentRecommendCompletionStatus> studentRecommendCompletionStatusList = studentRecommendInfoService.selectStudentRecommendCompletionStatus(year);
        vo.setStudentRecommendCompletionStatusList(studentRecommendCompletionStatusList);

        return AjaxResult.success(vo);
    }


    /**
     * 辅导员端，获取控制台首页数据
     *
     * @return
     */
    @RequestMapping("/counsellor/index/init")
    public AjaxResult counsellorInit() {
        String code = getUsername();
        CounsellorConsoleVo vo = counsellorStudentStatisticService.selectCounsellorIndexInfo(code);
        return AjaxResult.success(vo);
    }


}
